package com.ekuaitu.kuaitustaff.baiduMap;

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.ekuaitu.kuaitustaff.utils.DensityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


public class MapStatusUtils {
    private BaiduMap mBaiduMap;
    private Context mContext;
    private int screenWidth;
    private int screenHeight;

    public MapStatusUtils(BaiduMap mBaiduMap, Context mContext) {
        this.mBaiduMap = mBaiduMap;
        this.mContext = mContext;
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
    }

    public MapStatusUtils() {
    }


    /**
     * 非平滑移动地图的中心和缩放，地图中心在屏幕的中心
     *
     * @param targetPosition 地图中心
     * @param zoom           缩放范围 3~21
     */
    public void noAnimateMove(LatLng targetPosition, float zoom) {

        MapStatus mMapStatus = new MapStatus.Builder()
                .target(targetPosition)
                .targetScreen(new Point(screenWidth / 2, screenHeight / 2))
                .zoom(zoom)
                .build();
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(mMapStatus));
    }


    /**
     * 保持原来的中心的移动
     *
     * @param targetPosition 地图中心
     * @param zoom           缩放范围 3~21
     * @param duration       平滑移动的时间 毫秒值
     */
    public void animateMove(LatLng targetPosition, float zoom, int duration) {
        //获取屏幕高度宽度
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(targetPosition)
                .zoom(zoom)
                .build();
        if (duration == 0) {
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mMapStatus));
        } else {
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mMapStatus), duration);
        }
    }


    /**
     * 改变中心的移动,y==0表示移动至屏幕中心
     *
     * @param targetPosition 地图中心
     * @param zoom           缩放范围 3~21
     * @param duration       平滑移动的时间 毫秒值
     * @param y              地图中心在屏幕的Y坐标 ,Y等于0时，恢复默认的中心（屏幕中心）
     */
    public void animateMove(LatLng targetPosition, float zoom, int duration, int y) {
        MapStatus mMapStatus;
        if (y == 0) {
            mMapStatus = new MapStatus.Builder()
                    .target(targetPosition)
                    .targetScreen(new Point(screenWidth / 2, (screenHeight) / 2))
                    .zoom(zoom)
                    .build();
        } else {
            int layoutHeight = (int) DensityUtils.dp2px(mContext, y);
            mMapStatus = new MapStatus.Builder()
                    .target(targetPosition)
                    .targetScreen(new Point(screenWidth / 2, (screenHeight - layoutHeight) / 2))
                    .zoom(zoom)
                    .build();
        }

        if (duration == 0) {
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mMapStatus));
        } else {
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mMapStatus), duration);
        }

    }

    public void defaultPosition() {
        MapStatus.Builder builder = new MapStatus.Builder();
        LatLng center = new LatLng(39.915071, 116.403907); // 默认 天安门
        builder.target(center).zoom(18);
    }


    // 设置个性化地图config文件路径
    public void setMapCustomFile(Context context, String PATH) {
        FileOutputStream out = null;
        InputStream inputStream = null;
        String moduleName = null;
        try {
            inputStream = context.getAssets().open("customConfigdir/" + PATH);
            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);

            moduleName = context.getFilesDir().getAbsolutePath();
            File f = new File(moduleName + "/" + PATH);
            if (f.exists()) {
                f.delete();
            }
            f.createNewFile();
            out = new FileOutputStream(f);
            out.write(b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        MapView.setCustomMapStylePath(moduleName + "/" + PATH);
    }
}
